package com.chukun.spring.beans.defination;

import com.chukun.spring.beans.factory.DefaultUserFactory;
import com.chukun.spring.beans.factory.UserFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author chukun
 *  使用外部对象注册bean
 */
public class OuterSingletonBeanRegisterDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        UserFactory userFactory = new DefaultUserFactory();
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        beanFactory.registerSingleton("userFactory",userFactory);

        context.refresh();

        // 查找bean
        UserFactory bean = context.getBean(UserFactory.class);
        System.out.println(userFactory == bean);

        context.close();
    }
}
